<template>
  <tm-app>
    <tm-navbar title="易胜严选" :followTheme="true" :hideHome="true">
    </tm-navbar>
    <tm-sheet :padding="[0, 0]" :margin="[0, 0]" v-if="state.banners.length">
      <tm-carousel
        autoplay
        :round="0"
        dotPosition="bottom"
        align="right"
        :list="state.banners"
      ></tm-carousel>
    </tm-sheet>
    <tm-sheet
      :padding="[40]"
      :margin="[0, 0]"
      _class="flex flex-row flex-row-center-between"
      transprent
    >
      <tm-sheet
        :padding="[10]"
        :margin="[0, 0]"
        :round="4"
        class="flex-1"
        :followTheme="true"
        _class="flex flex-row flex-row-center-start"
        @click="navigatTo('createWifi')"
      >
        <tm-icon name="tmicon-ios-wifi" _class="mx-n4" :fontSize="50"></tm-icon>
        <tm-sheet :padding="[15]" :margin="[0, 0]" :followTheme="true">
          <tm-text :fontSize="32">创建WIFI码</tm-text>
          <tm-text :fontSize="20" class="mt-n2">一键连接 安全防蹭网</tm-text>
        </tm-sheet>
      </tm-sheet>
      <view class="mx-n5"></view>
      <tm-sheet
        :padding="[10]"
        :margin="[0, 0]"
        :round="4"
        :followTheme="true"
        class="flex-1"
        _class="flex flex-row flex-row-center-start"
        @click="navigatTo('wifiManager')"
      >
        <tm-icon name="tmicon-account" _class="mx-n4" :fontSize="50"></tm-icon>
        <tm-sheet :padding="[15]" :margin="[0, 0]" :followTheme="true">
          <tm-text :fontSize="32">我的WIFI码</tm-text>
          <tm-text :fontSize="20" class="mt-n2">查看管理只能WIFI码</tm-text>
        </tm-sheet>
      </tm-sheet>
    </tm-sheet>

    <!-- <tm-sheet
      :padding="[30, 30]"
      :margin="[0, 0]"
      color="#ffffff"
      _class="flex flex-row flex-row-center-start"
    >
      <tm-divider
        :followTheme="true"
        :border="10"
        real-color
        vertical
        :height="40"
      ></tm-divider>
      <tm-text :fontSize="36" :followTheme="true">平台福利</tm-text>
    </tm-sheet>

    <tm-sheet :padding="[20]" :margin="[0, 0]" :transprent="true">
      <tm-cell
        :margin="[0, 0]"
        :titleFontSize="30"
        :showAvatar="true"
        v-for="item in state.platformWelfare"
        :key="item.name"
        @click="navigatTo(item.routerName)"
      >
        <template v-slot:title>
          <tm-text :fontSize="32">{{ item.name }}</tm-text>
          <tm-text :fontSize="24" color="#666" _class="mt-n4">{{
            item.subName
          }}</tm-text>
        </template>
        <template v-slot:avatar>
          <tm-icon
            :name="item.icon"
            :font-size="40"
            :follow-theme="true"
          ></tm-icon>
        </template>
      </tm-cell>
    </tm-sheet> -->

    <tm-sheet
      :padding="[30, 30]"
      :margin="[0, 0]"
      color="#ffffff"
      _class="flex flex-row flex-row-center-start"
    >
      <tm-divider
        :followTheme="true"
        :border="10"
        real-color
        vertical
        :height="40"
      ></tm-divider>
      <tm-text :fontSize="36" :followTheme="true">平台服务</tm-text>
    </tm-sheet>

    <tm-sheet :padding="[20]" :margin="[0, 0]" :transprent="true">
      <tm-cell
        :margin="[0, 0]"
        :titleFontSize="30"
        :showAvatar="true"
        v-for="item in state.listSpecial"
        :key="item.name"
        @click="onClickPlatformService"
      >
        <template v-slot:title>
          <tm-text :fontSize="32">{{ item.name }}</tm-text>
          <tm-text :fontSize="24" color="#666" _class="mt-n4">{{
            item.subName
          }}</tm-text>
        </template>
        <template v-slot:avatar>
          <tm-icon
            :name="item.icon"
            :font-size="40"
            :follow-theme="true"
          ></tm-icon>
        </template>
      </tm-cell>
    </tm-sheet>

    <tm-sheet
      :padding="[30, 30]"
      :margin="[0, 0]"
      color="#ffffff"
      _class="flex flex-row flex-row-center-start"
    >
      <tm-divider
        :followTheme="true"
        :border="10"
        real-color
        vertical
        :height="40"
      ></tm-divider>
      <tm-text :fontSize="36" :followTheme="true">全国招商</tm-text>
    </tm-sheet>

    <tm-sheet :padding="[20]" :margin="[0, 0]" :transprent="true" :round="20">
      <tm-cell
        :margin="[0, 0]"
        :titleFontSize="30"
        :showAvatar="true"
        v-for="item in state.listMore"
        :key="item.name"
        @click="joinUs(item.key)"
      >
        <template v-slot:title>
          <tm-text :fontSize="32">{{ item.name }}</tm-text>
          <tm-text :fontSize="24" color="#666" _class="mt-n4">{{
            item.subName
          }}</tm-text>
        </template>
        <template v-slot:avatar>
          <tm-icon
            :name="item.icon"
            :font-size="40"
            :follow-theme="true"
          ></tm-icon>
        </template>
      </tm-cell>
    </tm-sheet>
  </tm-app>
</template>

<script setup>
import { onLoad, onReady } from "@dcloudio/uni-app";
import { reactive } from "vue";
import { useTmpiniaStore } from "@/tmui/tool/lib/tmpinia";
import { getSysBannerApi, signStatusApi, getInformationApi } from "@/api";
import { useUserStore } from "@/store";
const store = useTmpiniaStore();
const userStore = useUserStore();
const state = reactive({
  modelShow: true,
  banners: [],
  platformWelfare: [
    {
      routerName: "signIn",
      name: "签到",
      subName: "签到领现金，最高每天可领取3元",
      icon: "tmicon-flag",
    },
  ],
  listSpecial: [
    {
      routerName: "",
      name: "商务合作",
      subName: "诚邀各行业老板深度合作共赢",
      icon: "tmicon-transanction",
    },
    {
      routerName: "",
      subName: "诚聘志同道合朋友一起共事",
      name: "招贤纳士",
      icon: "tmicon-scan",
    },
    {
      routerName: "",
      subName: "拓客更高效留存更省力",
      name: "异业联盟",
      icon: "tmicon-supervise",
    },
    {
      routerName: "",
      subName: "让每一次分享都是因为热爱",
      name: "全民联动",
      icon: "tmicon-process",
    },
  ],
  listMore: [
    {
      routerName: "joinUs",
      name: "我要做合伙人",
      subName: "在线自动升级为合伙人，获取更高收益",
      icon: "tmicon-account-plus",
      key: 2,
    },
  ],
  contactLink: {},
});

onReady(async () => {
  setTimeout(() => {
    // 在页面中定义插屏广告
    let interstitialAd = null;

    // 在页面onLoad回调事件中创建插屏广告实例
    if (uni.createInterstitialAd) {
      interstitialAd = uni.createInterstitialAd({
        adUnitId: "adunit-4efccdf05efdbcce",
      });
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err);
      });
    }
  }, 500);
  state.contactLink = await getInformationApi();
});

onLoad(async () => {
  store.setTmVuetifyTheme("indigo");
  const res = await getSysBannerApi();
  state.banners = res.map((item) => item.sysValue);
  // 判断是否已经登录
  if (userStore.token) {
    const res = await signStatusApi();
    if (res) return;
    uni.showModal({
      title: "签到提示",
      content: "签到可领现金",
      success: function (res) {
        if (res.confirm) {
          navigatTo("signIn");
        }
      },
    });
  } else {
    // uni.showModal({
    //   title: "登录提示",
    //   content: "登录签到可领现金",
    //   success: function (res) {
    //     if (res.confirm) {
    //       uni.switchTab({
    //         url: "/pages/my/my",
    //       });
    //     }
    //   },
    // });
  }
});

const navigatTo = (key) => {
  uni.navigateTo({
    url: "/homePages/" + key,
  });
};
const joinUs = (key) => {
  uni.navigateTo({
    url: "/homePages/joinUs?type=" + key,
  });
};

const onClickPlatformService = () => {
  uni.showModal({
    title: "联系方式",
    content: "招商电话(微信同步)" + state.contactLink.sysValue,
    success: function (res) {
      if (res.confirm) {
        uni.setClipboardData({
          data: state.contactLink.sysValue,
          success: () => {
            uni.showToast({
              icon: "success",
              title: "复制成功",
            });
          },
        });
      }
    },
  });
};
</script>

<style scoped></style>
